const docgenApi = require('vue-docgen-api')

class CompInfoUtil {
  /**
   * 获取.vue的组件的信息
   * @param {*} filePath
   * @return {Promise}
   * @memberof CompInfoUtil
   */
  static getInfo(filePath) {
    return new Promise((resolve, reject) => {
      let info = {
        // 组件名
        compName: '',

        // 组件介绍
        compDes: '',

        // 组件演示md字符串
        docsBlocks: [],

        // 组件属性列表
        propsList: [],

        // 组件事件列表
        eventList: [],

        // 组件函数列表
        methodsList: [],

        // 组件槽列表
        slotsList: []
      }
      docgenApi
        .parse(filePath)
        .then((componentInfo) => {
          // console.log('---->', componentInfo)
          info['compDes'] = componentInfo['description']
          info['compName'] = componentInfo['displayName']
          info['docsBlocks'] = componentInfo['docsBlocks']
          info['eventList'] = componentInfo['events']
          info['methodsList'] = componentInfo['methods']
          info['propsList'] = componentInfo['props']
          info['slotsList'] = componentInfo['slots']
          // componentInfo['tags']
          resolve(info)
        })
        .catch((error) => {
          console.error(error)
          reject(null)
        })
    })
  }
}

module.exports = CompInfoUtil
